#include <iostream>
#include <fstream>
#include "socket.hpp"


int main(int num,char* args[])
{
    if(num != 3)
    {
        std::cout << args[0] << " ip  port"<<std::endl;
        exit(1);
    }
    uint16_t port = std::stoi(args[2]);
    std::string ip = args[1];
    SocketInter::SockIn clientsock(port,ip);
    clientsock.InitSockaddr();
    
    while(true)
    {
        int sockfd = clientsock.Socket();

        std::string str;
        do
        {
        std::cout << "请输入需要查询的单词:";
        std::getline(std::cin,str);
        } while (str.empty());
        
        clientsock.Connect();

        if(write(sockfd,str.c_str(),str.size()) < 0)//发送请求
        {
            std::cout << "发送失败" <<std::endl;
            exit(1);
        }
       
        char arr[10000];
        int n = read(sockfd,arr,sizeof(arr));
        if(n <= 0)
        {
            std::cout << "接受失败" <<std::endl;
            exit(2);
        }
        
        arr[n] = 0;
        std::cout << arr << std::endl;

        close(sockfd);
    }
    
    

    return 0;
}